<?php

namespace app\admin\controller;
use think\Db;
use app\common\service\UploadService;

class Admin extends Common{
	/*
	编辑管理员资料
	*/
	public function edit(){
		if(request()->method() == 'POST') {
			if ($this->admin_info['level'] != 1) {
				return ['code'=>1,'msg'=>'权限不足'];
			}
			$name     = request()->param('name');
			$password = request()->param('password');
			$kf1 = request()->param('kf1')?:'';
			$kf2 = request()->param('kf2')?:'';
			// $pic = request()->param('pic');
			$data     = [];
			if (empty($name)) {
				return ['code'=>0,'msg'=>'用户名不能为空'];
			}
			if ($password) {
				if(preg_match("/^[A-Za-z0-9]+$/", $password) == 0){
					return ['code'=>0,'msg'=>'密码只能为数字或字母组合'];
				}
				$data['password'] = md5($password);
			}
			$data['name'] = $name;
			// $data['kf1'] = $kf1;
			// $data['kf2'] = $kf2;
			// $data['pic'] = $pic;
			$res = Db::table('tp_admin')->where('id',$this->uid)->update($data);
			if ($res) {
				//更新session
				$user_info = Db::table('tp_admin')->where('id',$this->uid)->field('id,name')->find();
				session('user_info',$user_info);
				return ['code'=>0,'msg'=>'修改成功'];
			} else {
				return ['code'=>1,'msg'=>'修改失败'];
			}	
		} else {
			return $this->fetch();
		}
	}

	/*
	添加管理员资料
	*/
	public function add(){
		if (request()->method() == 'POST') {
			if ($this->admin_info['level'] != 1) {
				return ['code'=>1,'msg'=>'权限不足'];
			}
			$id = request()->param('id');
			$data = [];
			$data['name'] = request()->param('name');
			$data['level'] = request()->param('level');
			$data['data_uid'] = request()->param('data_uid');
			$password = request()->param('password');
			$data['remark'] = request()->param('remark');
			$data['kf1'] = request()->param('kf1')?:'';
			$data['kf2'] = request()->param('kf2')?:'';
			$data['kf3'] = request()->param('kf3')?:'';
			if (!in_array($data['level'], ['1','2','3','4'])) {
				return ['code'=>1,'msg'=>'等级有误'];
			}
			if ($this->admin_info['data_uid']) {
				$x_uids = Db::table('tp_user_team')->where('uid',$this->admin_info['data_uid'])->column('x_uid');
				$x_uids[] = $this->admin_info['data_uid'];
				if (!in_array($data_uid, $x_uids)) {
					return ['code'=>1,'msg'=>'只能展示伞下用户数据'];
				}
			}
			if ($id) {
				if ($password) {
					$data['password'] = md5($password);
				}
				$res = Db::table('tp_admin')->where('id',$id)->update($data);
			} else {
				$data['password'] = $password;
				$data['authority'] = 1;
				$data['type'] = 1;
				$data['pic'] = '/static/picture/admin-pic.jpg';
				$data['pid'] = $this->uid;
				$data['create_time'] = date('Y-m-d H:i:s',time());
				$res = Db::table('tp_admin')->insert($data);
			}
			
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'fail'];
			}
		} else {
			$id = request()->param('id');
			if ($id) {
				$data = Db::table('tp_admin')->where('id',$id)->find();
			} else {
				$data = [];
				$data['level'] = 1;
			}
			$this->assign('data',$data);
			$this->assign('id',$id);
			return $this->fetch();
		}
	}

	/*
	删除账号
	 */
	public function delete(){
		$id = request()->param('id');
		$res = Db::table('tp_admin')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'success'];
		} else {
			return ['code'=>1,'msg'=>'fail'];
		}
	}
}